#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>

// jsoncpp库，使用时加编译选项 -ljsoncpp

int main() 
{
    Json::Value jsonObj1;
    jsonObj1["data_x"] = 100;
    jsonObj1["data_y"] = 200;

    Json::FastWriter fastWriter; //紧凑风格 {"k1":100,"k2":200}
    Json::StyledWriter styledWriter; //展开风格

    std::string jsonString = fastWriter.write(jsonObj1); //序列化
    std::cout << jsonString << std::endl;

    Json::Value jsonObj2;
    int data_x = 0;
    int data_y = 0;

    Json::Reader reader;
    bool result = reader.parse(jsonString, jsonObj2); //反序列化
    if(result == true) 
    {
        data_x = jsonObj2["data_x"].asInt();
        data_y = jsonObj2["data_y"].asInt();
    }

    std::cout << "data_x: " << data_x << ", data_y: " << data_y << std::endl;

    return 0;
}